package com.lp.guidepage.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.lp.guidepage.R;
import com.lp.guidepage.ui.fragment.LaunchFragment;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;
import utils.SPUtils;


/**
 * @authorAdministrator 2020/6/17 0017
 * desc:
 */
public class GuidePageActivity extends AppCompatActivity {

    private ViewPager viewpager;
    private CircleIndicator indicator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);
        viewpager = findViewById(R.id.launch_viewpager);
        indicator = findViewById(R.id.launch_indicator);
        initView();
    }

    private void initView() {
        ImmersionBar.with(this)
                .fitsSystemWindows(false)    //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .init();  //必须调用方可应用以上所配置的参数
        if (SPUtils.get(this, "launchKey", "0").toString().equals("1")) {
            startActivity(new Intent().setClassName(this, "com.xunyoutest.shop.ui.activity.SplashActivity"));
            finish();
            return;
        }
        SPUtils.put(this, "launchKey", "1");
        //构造适配器
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            fragments.add(new LaunchFragment().setFlag(i));
        }
        FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments);
        //设定适配器
        viewpager.setAdapter(adapter);
        indicator.setViewPager(viewpager);
    }

    public class FragAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments;

        FragAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

    }

}
